<?php

namespace Home\Controller;

use Think\Controller;

class ServiceapiController extends Controller
{
    /*
     * 平台服务Api
     */

    //获取service接口地址
    private function getBaseUrl()
    {
       // return "http://112.33.1.160:81/healthcare_service_restful/";               //测试服务器
        return "http://service.health.10086.cn:8083/healthcare_service_restful/";       //正式服务器
    }

    //获取operations接口地址
    private function getBaseUrlOpetations()
    {
       // return "http://112.33.1.160:81/healthcare_operations_restful/";   //测试环境
        return "http://health.10086.cn/healthcare_operations_restful/";     //正式环境
    }

    private function appKey()
    {
        return 'ZSTJ';
    }

    private function invokeKey()
    {
        return '[ZSTJ-S]240sgSOKlC';
    }

    //套餐商品列表
    public function getInfoPhysicalExaminationList()
    {
        // var_dump($_POST);exit();
        // echo "你哈";exit();
        $url = $this->getBaseUrl() . "s/infoPhysicalExamination/getInfoPhysicalExaminationList";
        // var_dump($url);exit();
        $data = array(
            'appKey' => $this->appKey(),
            'invokeKey' => $this->invokeKey(),
            'packageId' => $_POST['packageId'],
            'name' => $_POST['name'],
            'originalPrice' => $_POST['originalPrice'],
            'salePrice' => $_POST['salePrice'],
            'imgSmall  ' => $_POST['imgSmall'],
            'imgBig' => $_POST['imgBig'],
            'suitableGender' => $_POST['suitableGender'],
            'examinationItems' => $_POST['examinationItems'],
            'introduction' => $_POST['introduction'],
            'status' => $_POST['status'],
            'description' => $_POST['description'],
            'type' => $_POST['type'],
            'pageIndex' => $_POST['pageIndex'],
            'pageSize' => $_POST['pageSize'],
        );
        // var_dump($data);exit();
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        $return = curl_exec($ch);
        // var_dump($return);exit();
        curl_close($ch);
        $getinfo = get_object_vars(json_decode($return));
        echo $return;
        exit();
    }

    //套餐适用机构
    public function getInfoPepMcList()
    {
        $url = $this->getBaseUrl() . "s/infoPepMc/getInfoPepMcList";
        $data = array(
            'appKey' => $_POST['appKey'],
            'invokeKey' => $_POST['invokeKey'],
            'packageId' => $_POST['packageId'],
            'mcId' => $_POST['mcId'],
            'mcbId' => $_POST['mcbId'],
            'status' => $_POST['status'],
            'pageIndex' => $_POST['pageIndex'],
            'pageSize' => $_POST['pageSize'],
        );
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        $return = curl_exec($ch);
        curl_close($ch);
        $getinfo = get_object_vars(json_decode($return));
        echo $return;
        exit();
    }

    //机构详细信息
    public function getMedicalCenterBranchList()
    {

        // echo "nihao ";exit();
        $url = $this->getBaseUrl() . "s/medicalCenter/getMedicalCenterBranchList";
        // var_dump($url);exit();
        $data = array(
            'appKey' => $_POST['appKey'],
            'invokeKey' => $_POST['invokeKey'],
            'packageId' => $_POST['packageId'],
            'mcId' => $_POST['mcId'],
            'mcbId' => $_POST['mcbId'],
            'name' => $_POST['name'],
            'country' => $_POST['country'],
            'province' => $_POST['province'],
            'city' => $_POST['city'],
            'address' => '',
            'telephone' => '',
            'description' => '',
            'gpsLat' => '',
            'gpsLon' => '',
            'baiduMapUrl' => '',
            'status' => $_POST['status'],
            'pageIndex' => $_POST['pageIndex'],
            'pageSize' => $_POST['pageSize'],
        );

        // $data=array(
        //     'appKey'=>"ZSTJ",
        //     'invokeKey'=>'[ZSTJ-P]Q0hitfewWu',
        //     'mcbId'=>"73"
        //     );
        // var_dump($data);exit();
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        $return = curl_exec($ch);
        // var_dump($return);exit();
        curl_close($ch);
        $getinfo = get_object_vars(json_decode($return));
        echo $return;
        exit();
    }

    //体检机构
    public function getMedicalCenterList()
    {
        $url = $this->getBaseUrl() . "s/medicalCenter/getMedicalCenterList";
        $data = array(
            'appKey' => $_POST['appKey'],
            'invokeKey' => $_POST['invokeKey'],
            'mcId' => $_POST['mcId'],
            'name' => $_POST['name'],
            'icon' => $_POST['icon'],
            'description' => $_POST['description'],
            'tags' => $_POST['tags'],
            'email' => $_POST['email'],
            'status' => $_POST['status'],
                'pageIndex' => $_POST['pageIndex'],
            'pageSize' => $_POST['pageSize'],
        );
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        $return = curl_exec($ch);
        curl_close($ch);
        $getinfo = get_object_vars(json_decode($return));
        echo $return;
        exit();
    }

    //获取所有体检大项
    public function getInfoExaminationItemList()
    {
        $url = $this->getBaseUrl() . "s/infoExaminationItem/getInfoExaminationItemList";
//        echo "你好";exit();
        $data = array(
            'appKey' => $_POST['appKey'],
            'invokeKey' => $_POST['invokeKey'],
            'itemId' => $_POST['itemId'],
            'name' => $_POST['name'],
            'description' => $_POST['description'],
            'type' => $_POST['type'],
            'content' => $_POST['content'],
            'diagnosis' => $_POST['diagnosis'],
            'objective' => $_POST['objective'],
            'analysis' => $_POST['analysis'],
            'suggestion' => $_POST['suggestion'],
                'pageIndex' => $_POST['pageIndex'],
            'pageSize' => $_POST['pageSize'],
        );
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        $return = curl_exec($ch);
        curl_close($ch);
        $getinfo = get_object_vars(json_decode($return));
        echo $return;
        exit();
    }

    //获取所有体检小项
    public function getExaminationIndicatorList()
    {
        $url = $this->getBaseUrl() . "s/examinationIndicator/getExaminationIndicatorList";
        $data = array(
            'appKey' => $_POST['appKey'],
            'invokeKey' => $_POST['invokeKey'],
            'indicatorId' => $_POST['indicatorId'],
            'name' => $_POST['name'],
            'itemId' => $_POST['itemId'],
            'description' => $_POST['description'],
            'reasonOver' => $_POST['reasonOver'],
            'reasonUnder' => $_POST['reasonUnder'],
            'cure' => $_POST['cure'],
            'advice' => $_POST['advice'],
            'valueRange' => $_POST['valueRange'],
               'pageIndex' => $_POST['pageIndex'],
            'pageSize' => $_POST['pageSize'],
        );
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        $return = curl_exec($ch);
        curl_close($ch);
        $getinfo = get_object_vars(json_decode($return));
        echo $return;
        exit();
    }

    //短信验证
    public function sendAuthcodeByPhoneNumber()
    {
        $url = $this->getBaseUrl() . "s/sms/sendAuthcodeByPhoneNumber";
        $data = array(
            'appKey' => $_POST['appKey'],
            'invokeKey' => $_POST['invokeKey'],
            'phoneNumber' => $_POST['phoneNumber'],
            'templateNumber' => $_POST['templateNumber'],
        );
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        $return = curl_exec($ch);
        curl_close($ch);
        $getinfo = get_object_vars(json_decode($return));
        echo $return;
        exit();
    }

    //注册
    public function addAuthUser()
    {
        $url = $this->getBaseUrl() . "s/userService/addAuthUser";
        $data = array(
            'appKey' => $_POST['appKey'],
            'invokeKey' => $_POST['invokeKey'],
            'userUid' => $_POST['userUid'],
            'userName' => $_POST['userName'],
            'userPassword' => $_POST['userPassword'],
            'userPreferredMobile' => $_POST['userPreferredMobile'],
            'validateUserMobile' => $_POST['validateUserMobile'],
            'userEmail' => $_POST['userEmail'],
            'validateUserEmail' => $_POST['validateUserEmail'],
            'userUsertypeDict' => $_POST['userUsertypeDict'],
            'addUserUid' => $_POST['addUserUid'],
            'userNickName' => $_POST['userNickName'],
            'userIdCardNumber' => $_POST['userIdCardNumber'],
            'userGenderDict' => $_POST['userGenderDict'],
            'userWedlockDict' => $_POST['userWedlockDict'],
            'userBirthday' => $_POST['userBirthday'],
            'userNationDict' => $_POST['userNationDict'],
            'userTelephoneNumber' => $_POST['userTelephoneNumber'],
            'userPostalAddress' => $_POST['userPostalAddress'],
            'userPostalCode' => $_POST['userPostalCode'],
            'userFaxNumber' => $_POST['userFaxNumber'],
            'userQq' => $_POST['userQq'],
            'userLocationProvince' => $_POST['userLocationProvince'],
            'userLocationCity' => $_POST['userLocationCity'],
            'userLocationArea' => $_POST['userLocationArea'],
            'userLocationStreet' => $_POST['userLocationStreet'],
            'userCompany' => $_POST['userCompany'],
            'userDepart' => $_POST['userDepart'],
            'userRemark' => $_POST['userRemark'],
            'userTag' => $_POST['userTag'],
            'userSignature' => $_POST['userSignature'],
            'userDescription' => $_POST['userDescription'],
            'userAccountEndtime' => $_POST['userAccountEndtime'],
        );
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        $return = curl_exec($ch);
        curl_close($ch);
        $getinfo = get_object_vars(json_decode($return));
        echo $return;
        exit();
    }

    //登录
    public function verifyLogin()
    {
        $url = $this->getBaseUrl() . "s/auth/verifyLogin";
        $data = array(
            'appKey' => $_POST['appKey'],
            'invokeKey' => $_POST['invokeKey'],
            'salt' => $_POST['salt'],
            'loginName' => $_POST['loginName'],
            'loginPwd' => $_POST['loginPwd'],
        );
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        $return = curl_exec($ch);
        curl_close($ch);
        $getinfo = get_object_vars(json_decode($return));
        echo $return;
        exit();
    }

    //根据Uid获取用户信息
    public function getUserByUid()
    {
        $url = $this->getBaseUrl() . "s/userService/getUserByUid";
        $data = array(
            'appKey' => $this->appKey(),
            'invokeKey' => $this->invokeKey(),
            'uid' => $_POST['uid'],
        );
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        $return = curl_exec($ch);
        curl_close($ch);
        $getinfo = get_object_vars(json_decode($return));
        echo $return;
        exit();
    }

    //获取应用软件下载列表
    public function getToDownloadSoftwareList()
    {
        $url = $this->getBaseUrl() . "s/portal/getToDownloadSoftwareList";
        $data = array(
            'appKey' => $_POST['appKey'],
            'invokeKey' => $_POST['invokeKey'],
            'platformTypeDict' => $_POST['platformTypeDict'],
            'softwareName' => $_POST['softwareName'],
        );
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        $return = curl_exec($ch);
        curl_close($ch);
        echo $return;
        exit();
    }

    //更新当前软件版本的下载次数

    public function updateSoftwareDownloadNum()
    {
        $url = $this->getBaseUrl() . "s/portal/updateSoftwareDownloadNum";
        $data = array(
            'appKey' => $_POST['appKey'],
            'invokeKey' => $_POST['invokeKey'],
            'softwareId' => $_POST['softwareId'],
            'currentVersion' => $_POST['currentVersion'],
        );
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        $return = curl_exec($ch);
        curl_close($ch);
        echo $return;
        exit();
    }

    /*用户找回密码
     * 参数：invokeKey
     *  appKey
     *  uid
     *  userPwd
     */
    public function updateUserPwd()
    {
        $url="http://health.10086.cn/healthcare_service_restful/s/userService/updateUserPwd";
        $data = array(
            'invokeKey' => $_POST['invokeKey'],
            'appKey' => $_POST['appKey'],
            'uid' => $_POST['uid'],
            'userPwd' => $_POST['userPwd'],
        );
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        $return = curl_exec($ch);
        curl_close($ch);
        echo $return;
        exit();
    }


    public function purchaseInfoSync()
    {
//        $_POST['out_trade_no']="2015110200001000310062221474";    //测试数据
//        var_dump($_POST['out_trade_no']);exit();
        if (isset($_POST['out_trade_no'])) {
//            echo "你好";exit();
            $outTradeNo=$_POST['out_trade_no'];
            $purchase = M("Purchase");

            $itemPurchase =(object)$purchase->where("sn=$outTradeNo")->find();

            $url = $this->getBaseUrl()."s/infoPhysicalExamination/getInfoPhysicalExaminationList";

            $data=array(
                'appKey'=>$this->appKey(),
                'invokeKey'=>$this->invokeKey(),
                'packageId'=>$itemPurchase->item_id,
            );

            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_HEADER, 0);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
            $return = curl_exec($ch);
//            var_dump($return);exit();
            $getInfo = get_object_vars(json_decode($return));
            $itemPackage=json_decode($getInfo['jsonResult']);
            $itemPackage=(object)$itemPackage->dataList[0];
            curl_close($ch);

            $orderProductListArr=array(
                'amount'=>$itemPurchase->item_count,
                'originalPrice'=>$itemPackage->originalPrice,
                'originalTotalPrice'=>$itemPackage->originalTotalPrice,
                'discount'=>(int)(($itemPackage->salePrice/$itemPackage->originalPrice)*10),
                'remark'=>$itemPackage->name,
                'platformProductId'=>$itemPurchase->item_id,
                // 'platformProductId'=>"150070",
                'productName'=>$itemPackage->name,
                'serviceTypeDict'=>'21',
                'bossProductId'=>$itemPurchase->item_id,
                'startTime'=>$itemPurchase->add_date,
                'endTime'=>$itemPurchase->update_date,
            );
            $orderProductList=json_encode($orderProductListArr);
            $orderProductList="[".$orderProductList."]";
//            var_dump($orderProductList);exit();
            //向西门子平台同步信息，个人已支付订单接口
            $url = $this->getBaseUrlOpetations()."s/order/addPaidOrderInfoIndividualCustomer";
            $data = array(
                'appKey' => $this->appKey(),
                'invokeKey' => $this->invokeKey(),
                'bossOrderId' => $itemPurchase->item_id,                    //选填
                'productList' => $orderProductList,                    //必填
               'platformUserUid' => cookie("uid"),                  //业务平台用户ID,必填
                // 'platformUserUid' => "wanggang",                  //业务平台用户ID
                'originalPrice' => $itemPackage->originalPrice,                   //原价  必填
                'finalPrice' => $itemPackage->salePrice,                      //订单实际金额    必填
                'paymentStatusDict' => '0',               //支付状态代码               必填 0-支付成功，1-支付失败
                'platformFlowNo' => $itemPurchase->sn,                  //全网唯一支付流水ID      必填
                'transactionTime' => $itemPurchase->add_date,                 //交易时间                必填
                'paymentChannelDict'=>"0"
            );
           // print_r($data);exit();
            $ch = curl_init();
//            var_dump($url);exit();
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_HEADER, 0);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
            $return = curl_exec($ch);
//            return $return;
//            var_dump($return);exit();
            curl_close($ch);
            echo $return;exit();
        }
    }


    public function getUidFromSessionData()
    {
        $url = $this->getBaseUrl()."s/auth/getUidBySessionData";
//        var_dump($url);exit();
        $data = array(
            'appKey' => $this->appKey(),
            'invokeKey' => $this->invokeKey(),
            'remoteAppKey' => $this->appKey(),
            'sessionData' => $_POST['sessionData'],
        );
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        $return = curl_exec($ch);
        curl_close($ch);
        $getinfo = get_object_vars(json_decode($return));
        echo $getinfo['jsonResult'];
        exit();
    }


       //关键模块打点统计
    public function addOperationStatisticsData(){
        $url = $this->getBaseUrl() . "s/statistics/addOperationStatisticsData";
        // var_dump($url);exit();
        $data = array(
            'appKey' => $this->appKey(),
            'invokeKey' => $this->invokeKey(),
            'datas'=>$_POST['datas'],
        );

        // var_dump($data);exit();
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        $return = curl_exec($ch);
        // var_dump($return);exit();
        curl_close($ch);

        $getinfo = get_object_vars(json_decode($return));
        $getinfo=$getinfo['jsonResult'];
        $getinfo=json_decode($getinfo);
        if($getinfo->resultCode==0){
            echo 0;
        }else{
            echo 1;
        }
        exit();
    }


    //向指定手机号码发送短信
    public function sendShortMessageByPhoneNumber()
    {
        $url = $this->getBaseUrl() . "s/sms/sendShortMessageByPhoneNumber";
        $content = $_POST['content'];
        $sendTime = $_POST['sendTime'];
        $phoneNumbers=$_POST['phoneNumbers'];
        $data = array(
            'appKey' => $this->appKey(),
            'invokeKey' => $this->invokeKey(),
            'phoneNumbers' => $phoneNumbers,
            'content' => $content,
            'priority' => '1',
            'sendTime' => $sendTime,
        );
        // var_dump($data);exit();
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        $return = curl_exec($ch);
        curl_close($ch);

        $getinfo = get_object_vars(json_decode($return));
        exit();
    }



}
